<script setup lang="ts">
import { useRole } from "./utils/hook";
import {
  ref
} from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import multiple from "../multiple/index.vue";
import preview from "../preview/index.vue";
import ReDialogOrDrawer from "@/components/ReDialogOrDrawer";
import {
  deviceDetection

} from "@pureadmin/utils";

import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import Install from "@iconify-icons/ri/install-line";
import Delete from "@iconify-icons/ep/delete";
defineOptions({
  name: "SystemRole"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  isShow,
  loading,
  columns,
  dataList,
  handleDelete,
  pagination,
  onSearch,
  resetForm,
  selectedNum,
  onSelectionCancel,
  onbatchDel,
  isMultiple,
  formMultiple,
  handleMultiple,
  handleMultipleSubmit,
  openDialog,
  handleSync,
  isPreview,
  formPreview,
  handlePreview,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useRole(tableRef);
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="表名称：" prop="table_name">
        <el-input
          v-model="form.table_name"
          placeholder="请输入表名称"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar
      :class="[isShow && !deviceDetection() ? '!w-[60vw]' : 'w-full']"
      style="transition: width 220ms cubic-bezier(0.4, 0, 0.2, 1)"
      title=""
      :columns="columns"
      @refresh="onSearch"
    >
      <template #title>
        <el-button
          type="success"
           v-auth="'tools:code:loading'"
          :icon="useRenderIcon(Install)"
          @click="handleMultiple"
          >装载数据表</el-button
        >
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <div
          v-if="selectedNum > 0"
          v-motion-fade
          class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
        >
          <div class="flex-auto">
            <span
              style="font-size: var(--el-font-size-base)"
              class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
            >
              已选 {{ selectedNum }} 项
            </span>
            <el-button type="primary" text @click="onSelectionCancel">
              取消选择
            </el-button>
          </div>
          <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
            <template #reference>
              <el-button type="danger" text class="mr-1"   v-auth="'tools:code:del'"> 批量删除 </el-button>
            </template>
          </el-popconfirm>
        </div>
        <pure-table
          ref="tableRef"
          :data="dataList"
          :columns="dynamicColumns"
          showOverflowTooltip
          table-layout="auto"
          adaptive
          :loading="loading"
          :adaptiveConfig="{ offsetBottom: 108 }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          :size="size"
          :pagination="pagination"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
          @selection-change="handleSelectionChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="handlePreview(row)"
            >
              预览
            </el-button>
            <el-popconfirm
              :title="`同步会重置字段配置生成信息，确定同步吗?`"
              @confirm="handleSync(row)"
            >
              <template #reference>
                <el-button
                  v-auth="'tools:code:sync'"
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(Refresh)"
                >
                  同步
                </el-button>
              </template>
            </el-popconfirm>
            <el-button
              v-auth="'tools:code:edit'"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('修改', row)"
            >
              修改
            </el-button>
            <el-popconfirm
              :title="`是否确认删除用户编号为${row.id}的这条数据`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  v-auth="'tools:code:del'"
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <ReDialogOrDrawer
      v-model:visible="isMultiple"
      :title="'装载数据表'"
      :component-type="'dialog'"
      :form-model="formMultiple"
      :showFooter="true"
      :custom-content="multiple"
      @submit="handleMultipleSubmit"
    />

    <ReDialogOrDrawer
      v-model:visible="isPreview"
      :title="'预览'"
      :component-type="'dialog'"
      :form-model="formPreview"
      :showFooter="false"
      :custom-content="preview"
    />

  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
